#include <stdio.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include "testFile.h"
#include "FileUtil.h"

#include <cglm/types.h>
#include <cglm/call.h>

void framebuffer_size_callback(GLFWwindow *window, int width, int height);

void processInput(GLFWwindow *window);

const int SCR_WIDTH = 800; // 显示窗口的宽
const int SCR_HEIGHT = 600;// 显示窗口的高

int test_opengl();

void print_vec4(vec4 _vec4) {
    printf(" | %f, %f, %f, %f | %p \n", _vec4[0], _vec4[1], _vec4[2], _vec4[3], _vec4);
}

void print_mat4(mat4 _mat4) {
    printf(" -------------------------------------------\n");
    print_vec4(_mat4[0]);
    print_vec4(_mat4[1]);
    print_vec4(_mat4[2]);
    print_vec4(_mat4[3]);
    printf(" -------------------------------------------\n");
}

#define MATH_PI 3.1415926535897932384626433832802

void updateMVPMatrix(int angleX, int angleY, float scaleX, float scaleY) {
    angleX = angleX % 360;
    angleY = angleY % 360;
    //转化为弧度角
    float radiansX = MATH_PI / 180.0f * angleX;
    float radiansY = MATH_PI / 180.0f * angleY;
    // Projection matrix 投影矩阵
    mat4 projection = GLM_MAT4_ZERO_INIT;
    glm_ortho(-1.0f, 1.0f, -1.0f, 1.0f, 0.1f, 100.0f, projection);
    //glm::mat4 Projection = glm::frustum(-ratio, ratio, -1.0f, 1.0f, 4.0f, 100.0f);
    //glm::mat4 Projection = glm::perspective(45.0f,ratio, 0.1f,100.f);

    // View matrix 观察矩阵
    vec3 camera = {0.0f, 0.0f, 4.0f};
    vec3 looks = {0.0f, 0.0f, 0.0f};
    vec3 head = {0.0f, 1.0f, 0.0f};
    // View matrix
    mat4 view = GLM_MAT4_ZERO_INIT;
    glm_lookat(
            camera, // Camera is at (0,0,1), in World Space
            looks, // and looks at the origin
            head,  // Head is up (set to 0,-1,0 to look upside-down)
            view
    );

    // Model matrix 变换矩阵
    mat4 model = GLM_MAT4_IDENTITY_INIT;
    glm_scale(model, (vec3) {scaleX, scaleY, 1.0f});
    glm_rotate(model, radiansX, (vec3) {1.0f, .0f, .0f});
    glm_rotate(model, radiansY, (vec3) {0.0f, 1.0f, 0.0f});
    glm_translate(model, (vec3) {0.0f, 0.0f, 0.0f});

    mat4 mv, mvp;
    glm_mat4_mul(view, model, mv);
    glm_mat4_mul(projection, mv, mvp);
    print_mat4(mvp) ;
}

int main() {
    //studyFile();
    //test_opengl();
    updateMVPMatrix(1 , 1 , 50 , 50);
    return 0;
}

int test_opengl() {
    // 初始化版本等
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    // 初始化创建一个窗口
    GLFWwindow *window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
    if (window == NULL) { // 验证是否创建成功
        printf("Failed to create GLFW window");
        glfwTerminate();
        return -1;
    }
    // 设置当前创建好的窗口
    glfwMakeContextCurrent(window);
    // 设置窗口变化的回调
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
    // 初始化GLAD
    if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
        printf("Failed to initialize GLAD");
        return -1;
    }

    // 创建一个顶点着色起
    const char *vertex = NULL;
    vertex = openAndReadFile("D:\\cppApp\\clion\\openglc\\shader\\base_vertex.shader");
    if (!vertex) {
        return -1;
    }
    int vertexShader = glCreateShader(GL_VERTEX_SHADER);
    // 设置定点着色起程序
    glShaderSource(vertexShader, 1, &vertex, NULL);
    // 继续编译
    glCompileShader(vertexShader);
    // 检查编译结果
    int success;
    char infoLog[512];
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
    if (!success) {
        glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
        printf("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n");
    }
    //同上 片元着色起
    const char *fragment = NULL;
    fragment = openAndReadFile("D:\\cppApp\\clion\\openglc\\shader\\base_fragment.shader");
    if (!fragment) {
        return -1;
    }
    int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragment, NULL);
    glCompileShader(fragmentShader);
    // check for shader compile errors
    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
    if (!success) {
        glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
        printf("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n");
    }
    // 创建一个渲染管道
    int shaderProgram = glCreateProgram();
    // 和定点着色器链接
    glAttachShader(shaderProgram, vertexShader);
    // 和片元着色起链接
    glAttachShader(shaderProgram, fragmentShader);

    glLinkProgram(shaderProgram);
    // 检查
    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
    if (!success) {
        glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
        printf("ERROR::SHADER::PROGRAM::LINKING_FAILED\n");
    }
    // 完成并释放空间
    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

    // 设置顶点数组
    float vertices[] = {
            // x    y     z
            0.5f, 0.5f, 0.0f,  // top right
            0.5f, -0.5f, 0.0f,  // bottom right
            -0.5f, -0.5f, 0.0f,  // bottom left
            -0.5f, 0.5f, 0.0f   // top left
    };
    // 顶点索引 和顶点数组配合使用
    unsigned int indices[] = {  // note that we start from 0!
            0, 1, 3,  // first Triangle
            1, 2, 3   // second Triangle
    };

    unsigned int VBO, VAO, EBO;
    // 创建一个顶点缓冲区
    glGenVertexArrays(1, &VAO);
    // 创建一个顶点和顶点索引缓冲
    glGenBuffers(1, &VBO);
    glGenBuffers(1, &EBO);
    // 创建完成,需要绑定, 告诉状态机.现在操作的缓冲区为 VAO
    glBindVertexArray(VAO);
    // 绑定一个顶点缓冲 VBO 并设置数据
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    // 绑定一个顶点缓冲 VEO 索引 并设置数据
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    // 绑定使用顶点的规则
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *) 0);
    // 解绑
    glEnableVertexAttribArray(0);
    // 可以解绑
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    // 不可以解绑
    //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    // 可以将顶点缓冲区 解绑 ,因为已经有了该索引VAO 需要时直接VAO绑定即可
    glBindVertexArray(0);

    while (!glfwWindowShouldClose(window)) {
        // 电脑输入
        processInput(window);

        // 清除并设置背景色
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        // 清除颜色缓冲
        glClear(GL_COLOR_BUFFER_BIT);

        // 激活渲染管道
        glUseProgram(shaderProgram);
        // 绑定VAO 如果只有一个 也可以在循环外绑定一次即可
        glBindVertexArray(VAO);
        // 进行绘制
        //glDrawArrays(GL_TRIANGLES, 0, 6); // 根据顶点数组绘制
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // 根据顶点索引绘制
        // glBindVertexArray(0); // 如果下次循环还需要, 则不需要解绑 ,如果需要使用绘制其他顶点数据, 直接绑定其他的索引即可, 但是最终需要释放.

        // 交换缓冲区和轮询IO事件（按键/释放、鼠标移动等）
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    // 绘制结束需要释放 缓冲区 渲染管路
    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);
    glDeleteBuffers(1, &EBO);
    glDeleteProgram(shaderProgram);

    // 终止，清除以前分配的所有glfw资源
    glfwTerminate();
    return 0;
}

// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window) {
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, 1);
}

// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
    // make sure the viewport matches the new window dimensions; note that width and
    // height will be significantly larger than specified on retina displays.
    glViewport(0, 0, width, height);
}